using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PlatForm
{
  [Route("/SubApp/[controller]/[action]")]
  public class UserSyncController : BaseApiController
  {

    readonly IUserSyncBusiness _userBus;
   
    #region DI
    public UserSyncController(IUserSyncBusiness userBus)
    {
      _userBus = userBus;
    }
    #endregion


    #region 提交
    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [NoCheckJWT]
    [CheckBasicAuth]
    public async Task Create(UserSyncInputDTO input)
    {
      await _userBus.AddDataAsync(input);
    }

    /// <summary>
    /// 修改用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [NoCheckJWT]
    [CheckBasicAuth]
    public async Task Update(UserSyncInputDTO input)
    {
      await _userBus.UpdateDataAsync(input);
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpPost]
    [NoCheckJWT]
    [CheckBasicAuth]
    public async Task Delete(string id)
    {
      await _userBus.DeleteDataAsync(id);
    }

    /// <summary>
    /// 批量同步用户
    /// </summary>
    /// <param name="list">用户列表</param>
    /// <returns></returns>
    [HttpPost]
    [NoCheckJWT]
    [CheckBasicAuth]
    public async Task Bulk(List<UserSyncInputDTO> list)
    {
      foreach (var item in list)
      {
        if (_userBus.CheckExists(item.Id))
        {
          await _userBus.UpdateDataAsync(item);
        }
        else
        {
          await _userBus.AddDataAsync(item);
        }
      }
    }
    #endregion
    
  }
}
